package net.devkit.core.module.sys.controller;

import javax.annotation.Resource;

import net.devkit.common.toolkit.DevConstant;
import net.devkit.common.toolkit.DevTool;
import net.devkit.common.toolkit.Pager;
import net.devkit.common.toolkit.retval.RetcodeGenerator;
import net.devkit.common.toolkit.retval.RetcodeWrapper;
import net.devkit.core.annotation.Kaptcha;
import net.devkit.core.cache.SessionStorer;
import net.devkit.core.mail.MailEntity;
import net.devkit.core.mail.MailService;
import net.devkit.core.module.sys.service.UserServicer;
import net.devkit.core.pojo.db.Dept;
import net.devkit.core.pojo.db.User;
import net.devkit.core.pojo.helper.PagerHelper;
import net.devkit.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user/")
@Scope("request")
public class UserController extends BaseController {

	@Resource
	private UserServicer userServicer;
	@Resource
	private MailService mailService;
	
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public String manage(){
		return "user/manage";
	}
	
	@RequestMapping(value="page",method=RequestMethod.POST)
	@ResponseBody
	public Pager<User> page(PagerHelper ph){
		return userServicer.findUserPager(ph);
	}
	
	@RequestMapping(value="modify",method=RequestMethod.PUT)
	@ResponseBody
	public Integer modify(User user){
		userServicer.moidfyUserInfo(user);
		return RetcodeGenerator.RETVAL_SUCCESS;
	}
	
	/**
	 * 发送测试邮件
	 */
	@RequestMapping(value="mail",method=RequestMethod.GET)
	public void sendTestmail() {
		mailService.sendSimpleMailMessage("szy.foggy@gmail.com", "test", "szy.foggy@qq.com", "Hello!");
		
		MailEntity entity = new MailEntity();
		entity.setTarget("szy.foggy@gmail.com");
		entity.putData("name", "szy.foggy");
		entity.putData("array", new String[]{"A","B","C","D","E"});
		mailService.sendRemindMail(entity);
	}
	
	/**
	 * 去到登录页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login() {
		return "user/login";
	}

	/**
	 * 登录处理
	 * 
	 * @return
	 */
	@Kaptcha
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public ModelAndView loginhandle(String loginname, String passwd) {
		ModelAndView mav = new ModelAndView();
		RetcodeWrapper<User> retval = userServicer.login(loginname, passwd);
		Long retcode = retval.getRetcode();
		User user = retval.getRetobj();

		if (!retcode.equals(UserServicer.LOGIN_SUCCESS)) {// 如果登录失败
			mav.setViewName("redirect:/user/login.htm");

			if (retcode.equals(UserServicer.LOGIN_FAILED_LOGINNAME_NOTEXISTS)) {
				super.setSessionMessage("用户名不存在，请查正!");
			} else if (retcode.equals(UserServicer.LOGIN_FAILED_PASSWD_ERROR)) {
				super.setSessionMessage("密码输入错误，请查正!");
			} else if (retcode
					.equals(UserServicer.LOGIN_FAILED_ACCOUNT_DISABLE)) {
				super.setSessionMessage("对不起，您的帐户己被管理员禁用，请联系管理员!");
			}
			return mav;
		}

		// 用户登录成功，存入session
		Dept dept = user.getDept();
		// 用户
		super.obtainSession().setAttribute(DevConstant.SESSION_CURRENT_USER,
				user);
		// 部门
		super.obtainSession().setAttribute(DevConstant.SESSION_CURRENT_DEPT,
				dept);

		// 存入session缓存库
		SessionStorer.put(dept.getId(), user.getId(), super.obtainSession());

		super.setSessionMessage("用户登录成功！");
		mav.setViewName("redirect:/home.htm");

		// 为登录日志作准备
		return mav;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String home() {
		return "index";
	}

	/**
	 * 登出处理
	 * 
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logouthandle() {

		String key = SessionStorer
				.obtaionSessionMapKey(super.obtainCurrentDept().getId(), super
						.obtainCurrentUser().getId());
		SessionStorer.removeSessionMap(key);

		DevTool.clearSession(super.obtainSession());

		return "redirect:/user/login.htm";
	}

}
